<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body,
        html {
            width: 100%;
            height: 100%;
        }

        #box1 {
            width: 100%;
            height: 100%;
        }

        #box2 {
            width: 400px;
            height: 250px;
            background-color: #fff;
            position: absolute;
            left: 40%;
            top: 40%;
            display: none;
            z-index: 2;
            padding-left: 100px;
            padding-top: 50px;
        }

        #hbox {
            border-bottom: 1px solid #ccc;
            height: 50px;
        }

        #h {
            float: left;
            width: 50%;
            height: 50px;
        }

        #del {
            float: right;
            width: 50%;
            height: 50px;
            cursor: pointer;
        }
        p{
            color:red;
        }
    </style>
</head>

<body>
    <div id="box1">你好，欢迎来到国际安全系统，请<a id="btn1" href="">登录</a></div>
    <div id="box2">
        <div id="hbox">
            <h2 id="h">注册登录</h2>
            <h3 id="del">X</h3>
        </div>

        用户名：<input type="text" id="uname">
        <p id="p1"></p>
        <br>
        密码：<input type="text" id="upass">
        <p id="p2"></p>
        <br>
        <input id="input" type="checkbox">同意<a href="">保密协议</a>
        <br>
        <button id="btn2" disabled="disabled">登录</button>
    </div>
    <script>
        btn1.onclick = function () {
            box2.style.display = "block"
            box1.style.backgroundColor = "#ccc"
            box1.style.opacity = ".8"

            // 阻止a标签默认事件
            return false
        }
        // 点击同意协议才可登陆
        input.onclick = function () {
            if (input.checked == true) {
                btn2.disabled = false
            } else {
                btn2.disabled = "disabled"
            }
        }
        // 验证用户名
        uname.onblur = function () {
            var reg = /^[a-z][a-z0-9]{5,11}$/i
            
            if (reg.test(uname.value)) {
                p1.innerHTML = "好的"
            } else {
                p1.innerHTML = "用户名不合法"
            }
        }
        // 验证密码
        upass.onblur = function () {
            var reg = /^\S{6,18}$/i
        
            if (reg.test(upass.value)) {
                p2.innerHTML = "密码正确"
            } else {
                p2.innerHTML = "密码不合法"
            }
        }
        // x关闭弹出层
        del.onclick = function () {
            box2.style.display = "none"
            box1.style.backgroundColor = "#fff"
            box1.style.opacity = "1"
        }
        // esc关闭弹出层
        document.onkeyup = function (e) {
            if (e.keyCode == 27) {
                box2.style.display = "none"
                box1.style.backgroundColor = "#fff"
                box1.style.opacity = "1"
            }
        }
        // 拖拽
        hbox.onmousedown = function () {
            document.onmousemove = function (e) {
                x = e.clientX
                y = e.clientY
                box2.style.left = x + "px"
                box2.style.top = y + "px"
            }
        }
        // 鼠标抬起取消拖拽
        document.onmouseup = function () {
            document.onmousemove = null
        }
        btn2.onclick=function(){
            box1.innerHTML="你好,"+uname.value+"<button id='btn3'>退出</button>"
            box2.style.display = "none"
            box1.style.backgroundColor = "#fff"
            box1.style.opacity = "1"
            btn3.onclick=function(){
            box1.innerHTML=`
            你好，欢迎来到国际安全系统，请<a id="btn1" href="">登录</a></div>`
        }
        }
    </script>
</body>

</html>